﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ConfigVerify.cs" company="">
//   
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace AdminTool
{
    using System;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    using ExecutionTool.CopyDatabase;
    using ExecutionTool.Profile;

    public partial class ConfigVerify : UserControl, IProfileConfigurator
    {
        #region Constructors and Destructors

        public ConfigVerify()
        {
            this.InitializeComponent();

            Program.ExportEnablementChanged += (sender, args) =>
                {
                    if (!args.Enabled)
                    {
                        this.checkBoxEnableExportVerification.Checked = args.Enabled;
                    }
                };

            this.checkBoxEnableExportVerification.Checked = false;
        }

        #endregion

        #region Public Methods and Operators

        public void LoadProfile(Profile profile)
        {
            this.checkBoxEnableExportVerification.Checked = profile.Orchestration.VerifyExport;

            this.textBoxServerNamePrefix.Text = profile.TargetSqlAzureVerification.ServerNamePrefix;
            this.textBoxUsername.Text = profile.TargetSqlAzureVerification.UserName;
            this.textBoxPassword.Text = profile.TargetSqlAzureVerification.Password;

            this.ResetMessage();
            this.WriterMessage("Profile data loaded.");
        }

        public void SaveProfile(Profile profile)
        {
            profile.Orchestration.VerifyExport = this.checkBoxEnableExportVerification.Checked;

            profile.TargetSqlAzureVerification.ServerNamePrefix = this.textBoxServerNamePrefix.Text.Trim();
            profile.TargetSqlAzureVerification.UserName = this.textBoxUsername.Text.Trim();
            profile.TargetSqlAzureVerification.Password = this.textBoxPassword.Text.Trim();

            this.WriterMessage("Profile data saved.");
        }

        #endregion

        #region Methods

        private void ConfigVerify_Load(object sender, EventArgs e)
        {
        }

        private void ResetMessage()
        {
            this.textBoxDetectedDatabaseName.Text = string.Empty;
        }

        private void WriterMessage(string message)
        {
            this.textBoxDetectedDatabaseName.Text += message;
            this.textBoxDetectedDatabaseName.Text += Environment.NewLine;
        }

        private void buttonTestConnection_Click(object sender, EventArgs e)
        {
            var serverName = this.textBoxServerNamePrefix.Text.Trim();
            var userName = this.textBoxUsername.Text.Trim();
            var password = this.textBoxPassword.Text.Trim();

            var error = false;

            if (string.IsNullOrWhiteSpace(serverName))
            {
                this.WriterMessage("Please specify the server name prefix first.");
                error = true;
            }

            if (string.IsNullOrWhiteSpace(userName))
            {
                this.WriterMessage("Please specify the user name first.");
                error = true;
            }

            if (string.IsNullOrWhiteSpace(password))
            {
                this.WriterMessage("Please specify the password first.");
                error = true;
            }

            if (error)
            {
                return;
            }

            this.textBoxDetectedDatabaseName.Text = string.Empty;
            this.WriterMessage("Starting to test the location...");
            var adapter = new SqlAzureAdapter();
            Task.Factory.StartNew(
                () =>
                adapter.GetExistingDatabaseNames(new SqlAzureLocation { ServerNamePrefix = serverName, UserName = userName, Password = password })).
                ContinueWith(
                    t =>
                        {
                            if (t.IsFaulted && t.Exception != null)
                            {
                                this.WriterMessage("Test failed. -->" + t.Exception);
                            }
                            else
                            {
                                this.WriterMessage("Tested successfully.");

                                if (t.Result.Count > 0)
                                {
                                    this.WriterMessage("Existing databases:");

                                    foreach (var name in t.Result)
                                    {
                                        this.WriterMessage("\t" + name);
                                    }
                                }
                            }
                        });
        }

        private void checkBoxEnableExportVerification_CheckedChanged(object sender, EventArgs e)
        {
            var cb = (CheckBox)sender;
            this.groupBox1.Enabled = cb.Checked;
        }

        private void checkBoxTogglePassword_CheckedChanged(object sender, EventArgs e)
        {
            var cb = (CheckBox)sender;
            this.textBoxPassword.UseSystemPasswordChar = !cb.Checked;

            ////if (cb.Checked)
            ////{
            ////    cb.Text = "Hide clear text";
            ////}
            ////else
            ////{
            ////    cb.Text = "Show clear text";
            ////}
        }

        #endregion
    }
}
